1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
local M = {}
-- Shorten function names
local actions = require('telescope.actions')
local fb_actions = require('telescope').extensions.file_browser.actions
--local builtin = require("telescope.builtin")
--local utils = require("telescope.utils")
--local layout_actions = require("telescope.actions.layout")
--local themes = require('telescope.themes')
local actions_set = require('telescope.actions.set')
local actions_state = require('telescope.actions.state')
local finders = require('telescope.finders')
local pickers = require('telescope.pickers')
local config = require('telescope.config').values
require('telescope').setup({
defaults = {
vimgrep_arguments = {
'rg',
'--color=never',
'--no-heading',
'--with-filename',
'--line-number',
'--column',
'--smart-case',
'--hidden',
'--fixed-strings',
'--trim',
},
prompt_prefix = ' ',
selection_caret = ' ',
entry_prefix = ' ',
path_display = { 'tail' },
--path_display = { "truncate" },
--path_display = { "smart" },
file_ignore_patterns = {
'packer_compiled.lua',
'~/.config/zsh/plugins',
'zcompdump',
'%.DS_Store',
'%.git/',
'%.spl',
--"%.log",
'%[No Name%]', -- new files / sometimes folders (netrw)
'/$', -- ignore folders (netrw)
'node_modules',
'%.png',
'%.zip',
'%.pxd',
--"^.vim/",
'^.local/',
'^.cache/',
'^downloads/',
'^music/',
--"^node_modules/",
--"^undodir/",
},
mappings = {
i = {
['<C-n>'] = actions.cycle_history_next,
['<C-p>'] = actions.cycle_history_prev,
['<C-j>'] = actions.move_selection_next,
['<C-k>'] = actions.move_selection_previous,
--["<C-c>"] = actions.close,
['<Esc>'] = actions.close, -- close w/ one esc
--["<Esc>"] = "close", -- close w/ one esc
['<?>'] = actions.which_key, -- keys from pressing <C-/>
['<Down>'] = actions.move_selection_next,
['<Up>'] = actions.move_selection_previous,
['<CR>'] = actions.select_default,
['<C-x>'] = actions.select_horizontal,
['<C-y>'] = actions.select_vertical,
['<C-t>'] = actions.select_tab,
['<C-u>'] = actions.preview_scrolling_up,
['<C-d>'] = actions.preview_scrolling_down,
['<PageUp>'] = actions.results_scrolling_up,
['<PageDown>'] = actions.results_scrolling_down,
['<Tab>'] = actions.toggle_selection + actions.move_selection_worse,
['<S-Tab>'] = actions.toggle_selection + actions.move_selection_better,
['<C-q>'] = actions.send_to_qflist + actions.open_qflist,
['<M-q>'] = actions.send_selected_to_qflist + actions.open_qflist,
['<C-l>'] = actions.complete_tag,
['<C-_>'] = actions.which_key, -- keys from pressing <C-/>
--["<C-o>"] = function(prompt_bufnr)
-- local selection = require("telescope.actions.state").get_selected_entry()
-- local dir = vim.fn.fnamemodify(selection.path, ":p:h")
-- require("telescope.actions").close(prompt_bufnr)
-- -- Depending on what you want put `cd`, `lcd`, `tcd`
-- vim.cmd(string.format("silent lcd %s", dir))
--end,
},
n = {
--["cd"] = function(prompt_bufnr)
-- local selection = require("telescope.actions.state").get_selected_entry()
-- local dir = vim.fn.fnamemodify(selection.path, ":p:h")
-- require("telescope.actions").close(prompt_bufnr)
-- -- Depending on what you want put `cd`, `lcd`, `tcd`
-- vim.cmd(string.format("silent lcd %s", dir))
--end,
['<esc>'] = actions.close,
['<q>'] = actions.close,
['<CR>'] = actions.select_default,
['<C-x>'] = actions.select_horizontal,
['<C-y>'] = actions.select_vertical,
['<C-t>'] = actions.select_tab,
['<Tab>'] = actions.toggle_selection + actions.move_selection_worse,
['<S-Tab>'] = actions.toggle_selection + actions.move_selection_better,
['<C-q>'] = actions.send_to_qflist + actions.open_qflist,
['<M-q>'] = actions.send_selected_to_qflist + actions.open_qflist,
['j'] = actions.move_selection_next,
['k'] = actions.move_selection_previous,
['H'] = actions.move_to_top,
['M'] = actions.move_to_middle,
['L'] = actions.move_to_bottom,
['<Down>'] = actions.move_selection_next,
['<Up>'] = actions.move_selection_previous,
['gg'] = actions.move_to_top,
['G'] = actions.move_to_bottom,
['<C-u>'] = actions.preview_scrolling_up,
['<C-d>'] = actions.preview_scrolling_down,
['<PageUp>'] = actions.results_scrolling_up,
['<PageDown>'] = actions.results_scrolling_down,
['cd'] = function(prompt_bufnr)
local selection = require('telescope.actions.state').get_selected_entry()
local dir = vim.fn.fnamemodify(selection.path, ':p:h')
require('telescope.actions').close(prompt_bufnr)
-- Depending on what you want put `cd`, `lcd`, `tcd`
vim.cmd(string.format('silent lcd %s', dir))
end,
['?'] = actions.which_key,
--["<C-o>"] = function(prompt_bufnr)
-- local selection = require("telescope.actions.state").get_selected_entry()
-- local dir = vim.fn.fnamemodify(selection.path, ":p:h")
-- require("telescope.actions").close(prompt_bufnr)
-- -- Depending on what you want put `cd`, `lcd`, `tcd`
-- vim.cmd(string.format("silent lcd %s", dir))
--end,
},
},
},
preview = {
filesize_limit = 3,
timeout = 250,
},
selection_strategy = 'reset',
sorting_strategy = 'ascending',
scroll_strategy = 'limit',
color_devicons = true,
layout_strategy = 'horizontal',
layout_config = {
horizontal = {
height = 0.95,
preview_cutoff = 70,
width = 0.92,
preview_width = { 0.55, max = 50 },
},
bottom_pane = {
height = 12,
preview_cutoff = 70,
prompt_position = 'bottom',
},
},
find_files = {
--cwd = '%:p:h',
cwd = vim.fn.getcwd(),
prompt_prefix = ' ',
--hidden = true,
--no_ignore = false,
follow = true,
},
--pickers = {
-- live_grep = {
-- disable_coordinates = true,
-- layout_config = {
-- horizontal = {
-- preview_width = 0.55,
-- },
-- },
-- },
--},
--pickers = {
-- live_grep = {
-- mappings = {
-- i = {
-- ["<C-f>"] = ts_select_dir_for_grep,
-- },
-- n = {
-- ["<C-f>"] = ts_select_dir_for_grep,
-- },
-- },
-- },
--},
--pickers = {
--lsp_references = {
-- prompt_prefix='⬅️',
-- show_line=false,
-- trim_text=true,
-- include_declaration=false,
-- initial_mode = "normal",
--},
--lsp_definitions = {
-- prompt_prefix='➡️',
-- show_line=false,
-- trim_text=true,
-- initial_mode = "normal",
--},
--lsp_document_symbols = {
-- prompt_prefix='* ',
-- show_line = false,
--},
--treesitter = {
-- prompt_prefix=' ',
-- show_line = false,
--},
--keymaps = { prompt_prefix='? ' },
--oldfiles = { prompt_prefix=' ' },
--highlights = { prompt_prefix=' ' },
--git_files = {
-- prompt_prefix=' ',
-- show_untracked = true,
-- path_display = { "tail" },
--},
--buffers = {
-- prompt_prefix=' ',
-- ignore_current_buffer = true,
-- initial_mode = "normal",
-- sort_mru = true,
--},
--live_grep = {
-- cwd='%:p:h',
-- disable_coordinates=true,
-- prompt_title='Search in Folder',
-- prompt_prefix=' ',
--},
--spell_suggest = {
-- initial_mode = "normal",
-- prompt_prefix = "暈",
-- theme = "cursor",
-- layout_config = { cursor = { width = 0.3 } }
--},
--colorscheme = {
-- enable_preview = true,
-- prompt_prefix = '',
-- results_title = '',
-- layout_strategy = "bottom_pane",
--},
--},
extensions = {
file_browser = {
theme = 'dropdown',
-- disables netrw and use telescope-file-browser in its place
hijack_netrw = false,
mappings = {
-- your custom insert mode mappings
['i'] = {
['<C-w>'] = function()
vim.cmd('normal vbd')
end,
['<C-h>'] = fb_actions.goto_parent_dir,
},
['n'] = {
-- your custom normal mode mappings
['N'] = fb_actions.create,
['<C-h>'] = fb_actions.goto_parent_dir,
--["/"] = function()
-- vim.cmd("startinsert")
--end,
},
},
},
},
})
--------------------------------------------------------------------------------
-- Load extensions:
-- have to be loaded after telescope setup/config
require('telescope').load_extension('fzf')
require('telescope').load_extension('ui-select')
require('telescope').load_extension('file_browser')
require('telescope').load_extension('changed_files')
require('telescope').load_extension('media_files')
require('telescope').load_extension('notify')
require('telescope').load_extension('dap')
require('telescope').load_extension('session-lens')
require('telescope').load_extension('flutter')
require('telescope').load_extension('recent_files')
--require('telescope').load_extension('projects')
--M.curbuf = function(opts)
-- opts = opts
-- or themes.get_dropdown({
-- previewer = false,
-- shorten_path = false,
-- border = true,
-- })
-- require("telescope.builtin").current_buffer_fuzzy_find(opts)
--end
function M.find_configs()
require('telescope.builtin').find_files({
hidden = true,
no_ignore = false,
prompt_title = ' Find Configs',
results_title = 'Config Files',
path_display = { 'smart' },
search_dirs = {
'~/.vim',
'~/.config/nvim',
'~/.config/zsh',
'~/.config/tmux',
'~/.config/X11',
'~/.config/alacritty',
'~/.config/kitty',
'~/.config/wezterm',
'~/.config/bspwm',
'~/.config/sxhkd',
'~/.config/picom',
'~/.config/polybar',
'~/.bashrc',
'~/.profile',
'~/.zprofile',
'~/.gitconfig',
'~/.gitsubtrees',
'~/.gitignore',
'~/.editorconfig',
'~/.prettierrc.yml',
'~/.ssh',
'~/README.md',
},
file_ignore_patterns = {
'/nvim/startup.log',
'zsh/plugins',
'packer_compiled.lua',
'resurrect',
'tmux/plugins',
--"^~/.config/tmux/plugins",
'%.txt',
'.git/',
'autoload/plugged',
'plug.vim',
'zcompdump',
},
layout_strategy = 'horizontal',
layout_config = { preview_width = 0.65, width = 0.75 },
})
end
function M.find_scripts()
require('telescope.builtin').find_files({
hidden = true,
no_ignore = true,
prompt_title = ' Find Scripts',
path_display = { 'smart' },
search_dirs = {
'~/.local/bin/scripts',
},
layout_strategy = 'horizontal',
layout_config = { preview_width = 0.65, width = 0.75 },
})
end
--function M.find_projects() -- aka Workspaces
-- require('telescope.builtin').find_files({
-- hidden = true,
-- no_ignore = true,
-- prompt_title = ' Find Projects',
-- path_display = { 'smart' },
-- search_dirs = {
-- '~/projects',
-- },
-- layout_strategy = 'horizontal',
-- layout_config = { preview_width = 0.65, width = 0.75 },
-- })
--end
function M.find_projects()
local search_dir = '~/projects'
pickers
.new({}, {
prompt_title = 'Change Directory',
finder = finders.new_oneshot_job({
'find',
vim.fn.expand(search_dir),
'-type',
'd',
'-maxdepth',
'4',
}),
previewer = require('telescope.previewers').vim_buffer_cat.new({}),
sorter = config.generic_sorter({}),
attach_mappings = function(prompt_bufnr, map)
actions_set.select:replace(function()
local entry = actions_state.get_selected_entry()
if entry ~= nil then
local dir = entry.value
actions.close(prompt_bufnr, false)
vim.fn.chdir(dir)
vim.cmd('e .')
vim.cmd("echon ''")
print('cwd: ' .. vim.fn.getcwd())
end
end)
return true
end,
})
:find()
end
function M.grep_notes()
local opts = {}
opts.hidden = false
opts.search_dirs = {
'~/documents/notes/',
}
opts.prompt_prefix = ' '
opts.prompt_title = ' Grep Notes'
opts.path_display = { 'smart' }
require('telescope.builtin').live_grep(opts)
end
function M.find_notes()
require('telescope.builtin').find_files({
hidden = true,
no_ignore = false,
prompt_title = ' Find Notes',
path_display = { 'smart' },
search_dirs = {
'~/documents/notes/private/',
'~/documents/notes',
'~/notes/private',
'~/notes',
},
layout_strategy = 'horizontal',
layout_config = { preview_width = 0.65, width = 0.75 },
})
end
function M.find_books()
require('telescope.builtin').find_files({
hidden = true,
no_ignore = false,
prompt_title = ' Find Books',
path_display = { 'smart' },
search_dirs = {
'~/documents/books',
'~/books',
},
layout_strategy = 'horizontal',
layout_config = { preview_width = 0.65, width = 0.75 },
})
end
function M.file_explorer()
require('telescope.builtin').file_browser({
prompt_title = 'File Browser',
cwd = '~',
layout_strategy = 'horizontal',
})
end
function M.grep_current_dir()
local buffer_dir = require('telescope.utils').buffer_dir()
local opts = {
prompt_title = 'Live Grep in ' .. buffer_dir,
cwd = buffer_dir,
}
require('telescope.builtin').live_grep(opts)
end
--------------------------------------------------------------------------------
local dropdown = require('telescope.themes').get_dropdown({
hidden = true,
no_ignore = true,
previewer = false,
prompt_title = '',
preview_title = '',
results_title = '',
layout_config = {
--anchor = "S",
prompt_position = 'top',
},
})
-- File browser always relative to buffer
--local opts_file_browser = vim.tbl_extend('force', dropdown, {
-- path_display = { '%:p:h' },
--})
-- Set current folder as prompt title
local with_title = function(opts, extra)
extra = extra or {}
local path = opts.cwd or opts.path or extra.cwd or extra.path or nil
local title = ''
local buf_path = vim.fn.expand('%:p:h')
local cwd = vim.fn.getcwd()
if path ~= nil and buf_path ~= cwd then
title = require('plenary.path'):new(buf_path):make_relative(cwd)
else
title = vim.fn.fnamemodify(cwd, ':t')
end
return vim.tbl_extend('force', opts, {
prompt_title = title,
}, extra or {})
end
-- Find here
function M.findhere()
-- Open file browser if argument is a folder
local arg = vim.api.nvim_eval('argv(0)')
if arg and (vim.fn.isdirectory(arg) ~= 0 or arg == '') then
vim.defer_fn(function()
require('telescope.builtin').find_files(with_title(dropdown))
-- require'telescope.builtin'.find_files(require('telescope.themes').get_dropdown({
-- hidden = true,
-- results_title = '',
-- layout_config = { prompt_position = 'top' },
-- }))
end, 10)
end
end
-- Define the custom command findhere/startup
vim.cmd('command! Findhere lua require("plugins.telescope").findhere()')
--vim.cmd('command! Startup lua require("plugins.telescope").findhere()')
--vim.api.nvim_command('autocmd VimEnter * lua require("plugins/telescope").findhere()')
-- Find dirs
function M.find_dirs()
local root_dir = vim.fn.input('Enter the root directory: ')
-- Check if root_dir is empty
if root_dir == '' then
print('No directory entered. Aborting.')
return
end
local entries = {}
-- Use vim.fn.expand() to get an absolute path
local root_path = vim.fn.expand(root_dir)
local subdirs = vim.fn.readdir(root_path)
if subdirs then
for _, subdir in ipairs(subdirs) do
if vim.fn.isdirectory(root_path .. '/' .. subdir) == 1 then
table.insert(entries, subdir)
end
end
end
pickers
.new({}, {
prompt_title = 'Change Directory',
finder = finders.new_table({
results = entries,
}),
previewer = config.file_previewer({}),
sorter = config.generic_sorter({}),
attach_mappings = function(prompt_bufnr, map)
actions_set.select:replace(function()
local entry = actions_state.get_selected_entry()
if entry ~= nil then
local selected_subdir = entry.value
actions.close(prompt_bufnr, false)
local selected_path = root_path .. '/' .. selected_subdir
vim.fn.chdir(selected_path)
vim.cmd('e .')
vim.cmd("echon ''")
print('cwd: ' .. vim.fn.getcwd())
end
end)
return true
end,
})
:find()
end
return M
|